/**
 ******************************************************************************
 * @file    frontled.c
 * @author  wjf
 * @version v0.00
 * @date    2019-06-29 12:12
 * @brief
 * @note
 *
 ******************************************************************************
 */

#include "frontled.h"
#include "LPC51U68.h"
#include "fsl_common.h"
#include "fsl_gpio.h"
#include "fsl_iocon.h"
#include "fsl_inputmux.h"

#define IOCON_PIO_INPFILT_OFF       0x0100u   /*!< Input filter disabled */
#define IOCON_PIO_OPENDRAIN_DI        0x00u   /*!< Open drain is disabled */
#define IOCON_PIO_SLEW_STANDARD       0x00u   /*!< Standard mode, output slew rate control is enabled */
#define PIN0_IDX                         0u   /*!< Pin number for pin 0 in a port 0 */
#define PIN1_IDX                         1u   /*!< Pin number for pin 1 in a port 0 */
#define PIN9_IDX                         9u   /*!< Pin number for pin 9 in a port 1 */
#define PIN10_IDX                       10u   /*!< Pin number for pin 10 in a port 1 */
#define PIN29_IDX                       29u   /*!< Pin number for pin 29 in a port 0 */
#define PORT0_IDX                        0u   /*!< Port index */
#define PORT1_IDX                        1u   /*!< Port index */


/**
 * IOCON function and mode selection definitions
 * See the User Manual for specific modes and functions supported by the
 * various LPC11u6x pins.
 */
// #define IOCON_FUNC0             0x0				/*!< Selects pin function 0 */
// #define IOCON_FUNC1             0x1				/*!< Selects pin function 1 */
// #define IOCON_FUNC2             0x2				/*!< Selects pin function 2 */
// #define IOCON_FUNC3             0x3				/*!< Selects pin function 3 */
// #define IOCON_FUNC4             0x4				/*!< Selects pin function 4 */
// #define IOCON_FUNC5             0x5				/*!< Selects pin function 5 */
// #define IOCON_FUNC6             0x6				/*!< Selects pin function 6 */
// #define IOCON_FUNC7             0x7				/*!< Selects pin function 7 */
// #define IOCON_MODE_INACT        (0x0 << 3)		/*!< No addition pin function */
// #define IOCON_MODE_PULLDOWN     (0x1 << 3)		/*!< Selects pull-down function */
// #define IOCON_MODE_PULLUP       (0x2 << 3)		/*!< Selects pull-up function */
// #define IOCON_MODE_REPEATER     (0x3 << 3)		/*!< Selects pin repeater function */
// #define IOCON_HYS_EN            (0x1 << 5)		/*!< Enables hysteresis */
// #define IOCON_INV_EN            (0x1 << 6)		/*!< Enables invert function on input */
// #define IOCON_ADMODE_EN         (0x0 << 7)		/*!< Enables analog input function (analog pins only) */
// #define IOCON_DIGMODE_EN        (0x1 << 7)		/*!< Enables digital function (analog pins only) */
// #define IOCON_SFI2C_EN          (0x0 << 8)		/*!< I2C standard mode/fast-mode */
// #define IOCON_STDI2C_EN         (0x1 << 8)		/*!< I2C standard I/O functionality */
// #define IOCON_FASTI2C_EN        (0x2 << 8)		/*!< I2C Fast-mode Plus */
// #define IOCON_FILT_DIS          (0x1 << 8)		/*!< Disables noise pulses filtering (10nS glitch filter) */
// #define IOCON_OPENDRAIN_EN      (0x1 << 10)		/*!< Enables open-drain function */
// #define IOCON_S_MODE_0CLK       (0x0 << 11)		/*!< Bypass input filter */
// #define IOCON_S_MODE_1CLK       (0x1 << 11)		/*!< Input pulses shorter than 1 filter clock are rejected */
// #define IOCON_S_MODE_2CLK       (0x2 << 11)		/*!< Input pulses shorter than 2 filter clock2 are rejected */
// #define IOCON_S_MODE_3CLK       (0x3 << 11)		/*!< Input pulses shorter than 3 filter clock2 are rejected */
// #define IOCON_S_MODE(clks)      ((clks) << 11)	/*!< Select clocks for digital input filter mode */
// #define IOCON_CLKDIV(div)       ((div) << 13)	/*!< Select peripheral clock divider for input filter sampling clock, 0 -6 */

void front_led_init(void)
{
	CLOCK_EnableClock(kCLOCK_Iocon);                           /* Enables the clock for the IOCON block. 0 = Disable; 1 = Enable.: 0x01u */
  CLOCK_EnableClock(kCLOCK_Gpio1);                           /* Enables the clock for the GPIO1 port registers. 0 = Disable; 1 = Enable. */

  gpio_pin_config_t LED_BLUE_io_config = {                   /* PORT1 PIN9 (number: 29) GPIO configuration structure */
    .pinDirection = kGPIO_DigitalOutput,
    .outputLogic = 1U
  };
  GPIO_PinInit(GPIO, 1, 9, &LED_BLUE_io_config); /* Initialize GPIO functionality on pin PIO1_9 (number: 29) */

  const uint32_t LED_BLUE_pin_config = (
    IOCON_FUNC0 |                                        /* Pin is configured as PIO1_9 */
    IOCON_DIGITAL_EN |                                   /* Enables digital function */
    IOCON_PIO_INPFILT_OFF |                                  /* Input filter disabled */
    IOCON_PIO_SLEW_STANDARD |                                /* Standard mode, output slew rate control is enabled */
    IOCON_OPENDRAIN_EN
  );
  IOCON_PinMuxSet(IOCON, 1, 9, LED_BLUE_pin_config); /* PORT1 PIN9 (number: 29) is configured as PIO1_9 */
}

void front_led_proc(void)
{
  GPIO_PortToggle(GPIO, 1, 1U << 9);
}

